﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;


namespace ComLib.Web.Lib.Extensions
{
    /// <summary>
    /// Renders section with header/content
    /// </summary>
    public static class HtmlHelperExtensionsForSections
    {
        /// <summary>
        /// Renders a section with a header and content.
        /// </summary>
        /// <param name="htmlhelper"></param>
        /// <param name="header">Header for section</param>
        /// <param name="content">Content for section</param>
        /// <param name="sectionId">Optional Id for the section.</param>
        /// <param name="cssClassForSection">Optional css class for section</param>
        /// <param name="cssClassForHeader">Optional css class for header</param>
        /// <param name="cssClassForContent">Optional css class for content</param>
        /// <returns></returns>
        public static MvcHtmlString Section(this HtmlHelper htmlhelper, string header, string content,
            string sectionId = "", string cssClassForSection = "section", 
            string cssClassForHeader = "section_head", string cssClassForContent = "section_body")
        {
            string html = "";
            if (!string.IsNullOrEmpty(sectionId))
            {
                html = string.Format(
                       @"<div id=""{0}"" class=""{1}"">"
                     + @"  <span id=""{2}"" class=""{3}"">{4}</span>"
                     + @"  <div id=""{5}"" class=""{6}"">{7}</div>"
                     + @"</div>", 
                     sectionId, cssClassForSection,
                    sectionId + "_head", cssClassForHeader, header,
                    sectionId + "_body", cssClassForContent, content);
                return MvcHtmlString.Create(html);
            }
            html = @"<div class=""{0}"">"
                     + @"  <span class=""{1}"">{2}</span>"
                     + @"  <div class=""{3}"">{4}</div>"
                     + @"</div>";

            html = string.Format(html, cssClassForSection, cssClassForHeader, header, cssClassForContent, content);
            return MvcHtmlString.Create(html);
        }


        /// <summary>
        /// Renders only a section header that can be clicked for showing/hiding the content.
        /// </summary>
        /// <param name="htmlhelper"></param>
        /// <param name="header">Header text</param>
        /// <param name="contentId">Id of the content div to show/hide</param>
        /// <param name="sectionId">Optional id of the header</param>
        /// <param name="cssClassForHeader">Optional css class for the header</param>
        /// <returns></returns>
        public static MvcHtmlString SectionHeaderShowHide(this HtmlHelper htmlhelper, string header, string contentId, string sectionId = "", string cssClassForHeader = "section_head")
        {
            string html = "";
            if (!string.IsNullOrEmpty(sectionId))
            {
                html = string.Format(@"<span id=""{0}"" class=""{1}"" onclick=""$('#{2}').toggle();"">{3}</span>", 
                    sectionId, cssClassForHeader, contentId, header);
                return MvcHtmlString.Create(html);
            }
            
            html = string.Format(@"<span class=""{0}"" onclick=""$('#{1}').toggle();"">{2}</span>", 
                cssClassForHeader, contentId, header);
            return MvcHtmlString.Create(html);
        }


        /// <summary>
        /// Renders a section with a header and content.
        /// </summary>
        /// <param name="htmlhelper"></param>
        /// <param name="header">Header for section</param>
        /// <param name="content">Content for section</param>
        /// <param name="sectionId">Optional Id for the section.</param>
        /// <param name="cssClassForSection">Optional css class for section</param>
        /// <param name="cssClassForHeader">Optional css class for header</param>
        /// <param name="cssClassForContent">Optional css class for content</param>
        /// <returns></returns>
        public static MvcHtmlString SectionShowHide(this HtmlHelper htmlhelper, string header, string content,
            string sectionId, string cssClassForSection = "section",
            string cssClassForHeader = "section_head", string cssClassForContent = "section_body")
        {
            var html = string.Format(
                       @"<div id=""{0}"" class=""{1}"">"
                     + @"  <span id=""{2}"" class=""{3}"" onclick=""$('#{4}').toggle();"">{5}</span>"
                     + @"  <div id=""{6}"" class=""{7}"">{8}</div>"
                     + @"</div>",
                     sectionId, cssClassForSection,
                    sectionId + "_head", cssClassForHeader, sectionId + "_body", header,
                    sectionId + "_body", cssClassForContent, content);
            return MvcHtmlString.Create(html);
        }
    }
}
